///TITLE: Examining public support for comprehensive policy packages to tackle unhealthy food environments*****

clear all
 import delimited "/Users/simone/Downloads/Day 2/Exercise/Stata/WP1.1 Final Survey (Only Completes, Concat).csv", delimiter(comma)
 
 
*************************************Data cleaning and management***********************************************
 
****Eliminating speeders***********
 drop if durationinseconds < 600 // Drop participants who take less than 10 minutes to complete (speeders)
drop if (age==.&gender==.)

****Cleaning height and weight data***********
destring height weight, replace force
replace height = 180 in 517 
replace height = . in 875 //875, 1088 and 554 were clearly input incorrectly. Changed to missing values
replace weight = . in 875
replace weight = . in 1088
replace height = . in 1088
replace height = . in 554
replace weight = . in 554
replace weight = . in 765
replace weight = . in 64
replace weight = . in 156
replace weight = . in 263
replace weight = . in 568

*****missing values for gender and nutrition-related disease - eliminating non-binary or prefer not to say*******
mvdecode gender, mv(3=.a \ 4=.b) // generate missing values for 'non-binary' and 'I prefer not to say'for gender
mvdecode nutrition_disease_1 nutrition_disease_2 nutrition_disease_3 nutrition_disease_4, mv (3=.) // generate missing value for 'I prefer not to say' for nutrition disease

*****recode section********
recode age (2 = 1) (3 = 2) (4 = 3)(5 = 4)(6 = 5)(7 = 6) //recode age bc it was coded 2-7 instead of 1-6
recode nutrition_disease_1 nutrition_disease_2 nutrition_disease_3 nutrition_disease_4 (2 = 0) // recode nutrition disease so that '1' is presence of disease and '0' is no disease

***re-naming variables*****
rename nutrition_disease_1 hypertension //re-naming nutrition related diseases (four lines)
rename nutrition_disease_2 cholesterol
rename nutrition_disease_3 CVD
rename nutrition_disease_4 diabetes
rename fairness_ks inequality_ks // re-naming fairness to inequality (greater on the scale means higher perception of inequality)
rename fairness_vatinc inequality_vatinc
rename fairness_vatdec inequality_vatdec
rename fairness_tax inequality_tax
rename fairness_nuted inequality_nuted
rename fairness_public inequality_public

*******************************************Demographics*******************************************************

**calculating bmi and creating bmi categories**
gen heightmeters = height/100
gen heightsq = heightmeters^2
generate bmi = weight / heightsq
generate bmi_cat =.
replace bmi_cat = 0 if bmi<18.5 & bmi>1
replace bmi_cat = 1 if bmi>18.5 & bmi<25
replace bmi_cat = 2 if bmi>25 & bmi<30
replace bmi_cat = 3 if bmi>30 & bmi<65
replace bmi_cat = 4 if bmi==.

***employment categories - more informative*****
generate employment1_cat =.
replace employment1_cat = 0 if employment==1 | employment==2 //full or part time
replace employment1_cat = 1 if employment==3 | employment==4 //temporarily laid off or unemployed
replace employment1_cat = 2 if employment==5 //retired
replace employment1_cat = 3 if employment>5 //homemaker,disabled,student, other
tab employment1_cat

***dummy variable - employment yes/no ***
generate employment2_cat =.
replace employment2_cat = 0 if employment>3 | employment==3
replace employment2_cat = 1 if employment<3
tab employment2_cat

***dummy-variable - nutrition_related disease yes/no***
generate nutritiondisease_cat =.
replace nutritiondisease_cat = 0 if hypertension==0 | cholesterol==0 | CVD==0 | diabetes==0 // no nutrition-related disease
replace nutritiondisease_cat = 1 if hypertension==1 | cholesterol==1 | CVD==1 | diabetes==1 // yes nutrition-related disease

***dummy variables - political leaning - far left, left of center, center, right of center, far right***
generate politicallean_cat =.
replace politicallean_cat = 0 if political_leaning==1 | political_leaning==2 // far left
replace politicallean_cat = 1 if political_leaning==3 | political_leaning==4 // left of center
replace politicallean_cat = 2 if political_leaning==5 | political_leaning==6 // center
replace politicallean_cat = 3 if political_leaning==7 | political_leaning==8 //right of center
replace politicallean_cat = 4 if political_leaning==9 | political_leaning==10 //far right

***regional residence - east vs. west germany***
generate formergdr =.
replace formergdr = 0 if region==1 | region==2 | region==3 | region==5 | region==6 | region==7 | region==9 | region==10 | region==11 | region==12 | region==15
replace formergdr = 1 if region==4 | region==8 | region==13 | region==14 | region ==16 //Includes Mecklenburg-Vorpommern (8), Brandenburg (4), Sachsen (13), Sachsen-Anhalt (14), and Thüringen (16)

**sample characteristics summary**
tab gender
tab age
tab income
tab employment1_cat
tab employment2_cat
tab parental_status
tab bmi_cat
tab politicallean_cat
tab hypertension
tab cholesterol
tab CVD
tab diabetes
tab nutritiondisease_cat
tab formergdr   

//INPUT VARIABLES

*******************generating input variables for average policy-specific beliefs across policies****************
egen effectiveness = rowmean (effectiveness_vatinc effectiveness_tax effectiveness_ks effectiveness_water effectiveness_nuted effectiveness_public effectiveness_vatdec)
egen coerciveness = rowmean (coerciveness_vatinc coerciveness_tax coerciveness_ks coerciveness_water coerciveness_nuted coerciveness_public coerciveness_vatdec)
egen inequality = rowmean (inequality_vatinc inequality_tax inequality_ks inequality_water inequality_nuted inequality_public inequality_vatdec)
egen majority = rowmean (majority_vatinc majority_tax majority_ks majority_water majority_nuted majority_public majority_vatdec)

********generating globals****************************
global socios gender age income politicallean_cat //note to self: removed employment bc it didn't make sense
global healthdata nutritiondisease_cat bmi_cat
global policybeliefs_averages effectiveness coerciveness inequality majority 
global topicbeliefs awareness legitimacy social_norm 
global policybeliefs_tax effectiveness_tax coerciveness_tax inequality_tax majority_tax // globals for policy-specific beliefs for individual policies 
global policybeliefs_vatinc effectiveness_vatinc coerciveness_vatinc inequality_vatinc majority_vatinc 
global policybeliefs_vatdec effectiveness_vatdec coerciveness_vatdec inequality_vatdec majority_vatdec 
global policybeliefs_nuted effectiveness_nuted coerciveness_nuted inequality_nuted majority_nuted
global policybeliefs_ks effectiveness_ks coerciveness_ks inequality_ks majority_ks
global policybeliefs_water effectiveness_water coerciveness_water inequality_water majority_water
global policybeliefs_public effectiveness_public coerciveness_public inequality_public majority_public 

********logistic regressions - standardizing scale of demographic variables****************************
foreach var of varlist $socios $healthdata {
	egen `var'std = std(`var')
}
global sociosstd genderstd agestd incomestd politicallean_catstd
global healthdatastd nutritiondisease_catstd bmi_catstd

sum $sociosstd $healthdatastd


// BEGINNING WIDE TO LONG TRANSFORMATION OF DATA FOR LOGIT MODEL

****Expansion 1, manual expand****

*define globals*
global optouta_task optout_1a optout_2a optout_3a optout_4a optout_5a optout_6a optout_7a optout_8a optout_1b optout_2b optout_3b optout_4b optout_5b optout_6b optout_7b optout_8b
rename optout_1a optout1
rename optout_2a optout2
rename optout_3a optout3 
rename optout_4a optout4 
rename optout_5a optout5 
rename optout_6a optout6 
rename optout_7a optout7  
rename optout_8a optout8 
rename optout_1b optout9
rename optout_2b optout10
rename optout_3b optout11 
rename optout_4b optout12 
rename optout_5b optout13 
rename optout_6b optout14 
rename optout_7b optout15  
rename optout_8b optout16 

**generate unique id for each participant 
generate id= _n

**generate block**
generate block=0 if(optout5==.)
replace block=1 if (optout1==.)

**reshape 
reshape long optout, i(id) j(task) //

*************************Coding of choice set attributes according choice set design***************************

**attribute 1 - nutrition standards in kindergartens and schools (KS)**
sort id task  
egen KS=fill(1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1)

**attribute 2 - VAT increase (VATinc)**
sort id task
egen vatinc=fill(1 0 0 0 0 1 0 1 0 1 1 1 1 0 1 0 1 0 0 0 0 1 0 1 0 1 1 1 1 0 1 0)

**attribute 3 - VAT dec (VATdec)**
sort id task
egen vatdec=fill(1 0 0 1 0 1 1 0 0 1 1 0 1 1 0 0 1 0 0 1 0 1 1 0 0 1 1 0 1 1 0 0)

**attribute 4 - SSB tax (tax)**
sort id task
egen tax=fill(0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 )

**attribute 5 - nutrtion standards in public institutions (public)**
sort id task
egen public=fill(1 1 0 1 1 1 0 0 0 0 1 0 0 0 1 1 1 1 0 1 1 1 0 0 0 0 1 0 0 0 1 1 )

**attribute 6 - action plan on drinking water (water) **
sort id task
egen water=fill(0 0 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0 1 1)

**attribute 7 - nutrition education in kindergartens and schools **
sort id task
egen nuted=fill(1 0 1 1 1 1 1 1 0 1 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 0 0 0 0 )

****package A or package B*******
sort id task
egen package_A=fill(1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0) //this will be control variable in the regrssion to show that support for a given policy was not dependent on whether the policy was presented in policy package A or in policy package B 

*******REGRESSION - OPTOUT********

***recoding optout to binary 0 or 1****
replace optout = 0 if optout == 1 | optout == 2
replace optout = 1 if optout == 3 | optout == 4

******Logit fixed effects model********

//COEFPLOT
xtset id task
xtlogit optout vatinc tax water public KS nuted vatdec package_A, fe or
coefplot, eform xline(1) drop(package_A)
est sto opt_out_model
esttab opt_out_model using optout.rtf, se label replace wide eform

//MARGINS PLOT
xtset id task
xtlogit optout vatinc tax water public KS nuted vatdec package_A, fe or
est sto opt_out_model
margins, dydx (vatinc tax water public KS nuted vatdec) //
marginsplot, xline(0) recast(scatter) recastci(rspike) horizontal
esttab opt_out_model using optout.rtf, se label replace wide eform

